//
//  HDSMPenListVC.m
//  HDSmartPenSDKDemo
//
//  Created by 牛的mini on 2016/12/26.
//  Copyright © 2016年 牛的mini. All rights reserved.
//

#import "HDSMPenListVC.h"
#import "HDBeaconManager.h"
#import "HDSMPenSettingVC.h"

@interface HDSMPenListVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *beaconArray;
@property (nonatomic, strong) HDBeaconManager *beaconManager;

@end

@implementation HDSMPenListVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _myTableView.rowHeight = 65.0f;
    self.title = @"智慧笔";
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0.0, 40, 40);
    [rightButton setImage:[UIImage imageNamed:@"ic_more"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    [self.navigationItem setRightBarButtonItem:rightBarButtonItem];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(beaconReloadAction:)
                                                 name:kScan_Beacon_And_Reload_Notification
                                               object:nil];
    
    _beaconManager = [HDBeaconManager sharedBeacon];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [_beaconManager startBeacon];
    [LCCoolHUD showLoading:@"正在扫描" inView:self.view];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_beaconManager stopScanAction];
}

- (void)beaconReloadAction:(NSNotification *)sender
{
    self.beaconArray = sender.object;
    [_myTableView reloadData];
    [LCCoolHUD hideInView:self.view];

}

- (void)rightBtnAction
{ [self performSegueWithIdentifier:@"HDSMPenListVCPushToMoreLine" sender:nil]; }

#pragma mark
#pragma mark -------- TableView Delegate --------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return self.beaconArray.count; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger nowRow = indexPath.row;
    static NSString *identifier = @"HDSMPenListVCCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.textLabel.text = @"SM_Pen";
    cell.detailTextLabel.text = @"Num:1";
    cell.imageView.image = [UIImage imageNamed:@"main_icon"];
    
    if (self.beaconArray.count > nowRow)
    {
        PeripheralModel *info = self.beaconArray[nowRow];
        cell.textLabel.text = info.discoveredPeripheral.name;
        cell.detailTextLabel.text = info.discoveredPeripheral.identifier.UUIDString;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HDDeclare *declare = [HDDeclare sharedDeclare];
    declare.peripheralObj = self.beaconArray[indexPath.row];
    [self performSegueWithIdentifier:@"SMPenListPushToSettingLine" sender:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
